package com.jrx.anytxn.accounting.job.glinterface.step;

import com.jrx.anytxn.accounting.bean.GlInterfaceBean;
import com.jrx.anytxn.accounting.entity.GaVoucherInfo;
import com.jrx.anytxn.accounting.entity.TlGlInterfaceInfoExcpReport;
import com.jrx.anytxn.accounting.mapper.ext.ExtTlGlInterfaceInfoExcpReportMapper;
import com.jrx.anytxn.accounting.mapper.ext.ExtVoucherInfoMapper;
import org.springframework.batch.item.ItemWriter;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yf
 * date  2019/9/11 0011
 * @ClassName GlInterfaceWriter
 * 总账流水写处理
 * @Version V1.0
 **/
@Service
public class GlInterfaceWriter implements ItemWriter<GlInterfaceBean> {

    @Resource
    private ExtVoucherInfoMapper extVoucherInfoMapper;
    @Resource
    private ExtTlGlInterfaceInfoExcpReportMapper exttlGlInterfaceInfoExcpReportMapper;

    @Override
    public void write(List items) {

        List<GaVoucherInfo> voucherInfoList = new ArrayList<>();
        List<TlGlInterfaceInfoExcpReport> tlGlInterfaceInfoExcpReportList = new ArrayList<>();

        for (Object item : items) {
            GlInterfaceBean bean = (GlInterfaceBean) item;
            if (!CollectionUtils.isEmpty(bean.getVoucherInfoList())) {
                voucherInfoList.addAll(bean.getVoucherInfoList());
            }
            if (!CollectionUtils.isEmpty(bean.getTlGlInterfaceInfoExcpReportList())) {
                tlGlInterfaceInfoExcpReportList.addAll(bean.getTlGlInterfaceInfoExcpReportList());
            }
        }

        if (!CollectionUtils.isEmpty(voucherInfoList)) {
            extVoucherInfoMapper.batchInsert(voucherInfoList);
        }

        if (!CollectionUtils.isEmpty(tlGlInterfaceInfoExcpReportList)) {
            exttlGlInterfaceInfoExcpReportMapper.batchInsert(tlGlInterfaceInfoExcpReportList);
        }
    }

}



